package com.shengyin.ec.service.goods;

import java.util.ArrayList;
import java.util.List;

import org.compass.core.CompassCallback;
import org.compass.core.CompassException;
import org.compass.core.CompassHits;
import org.compass.core.CompassSession;

import com.shengyin.ec.model.entity.Goods;

public class SearchCallback implements CompassCallback<List<Goods>>{
	private String keyword;
	private int firstResult;
	private int maxResult;

	public SearchCallback(String keyword, int firstResult, int maxResult) {
		this.firstResult = firstResult;
		this.maxResult = maxResult;
		this.keyword = keyword;
	}
	@Override
	public List<Goods> doInCompass(CompassSession session) throws CompassException {
		CompassHits hits = session.find(keyword);
		//System.out.println(hits.length());
		List<Goods> list = new ArrayList<Goods>();
		int length = firstResult + maxResult;
		if(length>hits.length()) length = hits.length();
		for(int i = firstResult ; i < length ; i++){
			Goods goods = (Goods)hits.data(i);
			if(hits.highlighter(i).fragment("productName")!=null)
				goods.setProductName(hits.highlighter(i).fragment("productName"));
			if(hits.highlighter(i).fragment("description")!=null)
				goods.setDescription(hits.highlighter(i).fragment("description"));
			list.add(goods);
		}
		return list;
	}

}
